﻿using AuSys.Services.App.FileStorage;
using Microsoft.AspNetCore.Mvc;
using HeyRed.Mime;

namespace AuSys.Controllers.System
{
    public class StaticFileController(
        IFileStorage fileStorage
        ): ControllerBase
    {
        [Route(FileStorageConsts.ActionRoutePattern)]
        public IActionResult Get(string fileName)
        {
            string pathName = fileName;
            if (fileStorage.ProvideType == StorageProvideType.Stream)
            {
                var stream = fileStorage.Read(pathName);
                if (stream is null)
                    return NotFound();
                return File(stream, MimeTypesMap.GetMimeType(fileName));
            }
            else if (fileStorage.ProvideType == StorageProvideType.Redirect)
            {
                var url = fileStorage.FullUrl(pathName);
                return Redirect(url);
            }
            throw new Exception("未知静态文件提供方式");
        }
    }
}
